package com.lsec.springbootinit.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;


/**
 * Swagger配置 (openapi3)
 *
 * @author Cherry
 * @date 2023/06/08
 */
@Configuration
@Profile({"dev", "stating"})
public class SwaggerConfig {


    @Bean
    public OpenAPI customOpenAPI(SwaggerProperties properties) {
        return new OpenAPI()
                .info(buildInfo(properties))
                .externalDocs(externalDocs(properties))
                .components(components())
                .addSecurityItem(securityRequirement());
    }


    /**
     * API 摘要信息
     */
    private Info buildInfo(SwaggerProperties properties) {
        return new Info()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .version(properties.getVersion())
                .contact(new Contact().name(properties.getAuthor()).url(properties.getUrl()).email(properties.getEmail()))
                .license(new License().name(properties.getLicense()).url(properties.getLicenseUrl()));
    }


    /**
     * 组件
     *
     * @return {@link Components}
     */
    private Components components() {
        return new Components()
                .addSecuritySchemes(HttpHeaders.AUTHORIZATION,
                        new SecurityScheme()
                                .type(SecurityScheme.Type.HTTP)
                                // token 所在位置
                                .in(SecurityScheme.In.HEADER)
                                // 请求头的 name
                                .name("Bearer 认证")
                                // 类型
                                .scheme("bearer")
                                .bearerFormat("JWT")


                );

    }


    /**
     * 外部文档
     *
     * @param properties 属性
     * @return {@link ExternalDocumentation}
     */
    private ExternalDocumentation externalDocs(SwaggerProperties properties) {
        return new ExternalDocumentation()
                .description(properties.getDescription())
                .url(properties.getUrl());
    }


    /**
     * 安全性要求
     *
     * @return {@link SecurityRequirement}
     */
    private SecurityRequirement securityRequirement() {
        return new SecurityRequirement()
                // 对应上面的SecuritySchemes  Key
                .addList(HttpHeaders.AUTHORIZATION);
    }




    /**
     * 标签按字母顺序排序 (order注解必须使用des参数，不然识别不到)
     *
     * @return {@link GlobalOpenApiCustomizer}
     */
//    public GlobalOpenApiCustomizer sortTagsAlphabetically() {
//        return openApi -> Optional.ofNullable(openApi.getTags())
//                .ifPresent(tags -> openApi.setTags(openApi.getTags()
//                        .stream()
//                        .sorted(Comparator.comparing(Tag::getName))
//                        .collect(Collectors.toList())));
//    }


}